package cn.com.ylpw.web.common.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.ylpw.spring.SpringController;
import cn.com.ylpw.web.common.service.ICommonService;
import cn.com.ylpw.web.util.ServletUtil;
import cn.com.ylpw.web.util.StringUtil;

@Controller("commonController")
@RequestMapping("/nqt/common/")
public class CommonController extends SpringController {
	public final Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	@Qualifier("commonService")
	private ICommonService commonService;
	
	
	
	
	
	
	/**
	 * 加载顶部导航条
	 * 
	 * @author July july_sky@foxmail.com
	 * @date 2015年5月26日上午11:04:39
	 * @param params
	 * @return
	 */
	@RequestMapping("top")
	public ModelAndView getTopBanner(@RequestParam Map<String,Object> params,HttpSession session) {
		ModelAndView mv = new ModelAndView();
		super.clean(params);
		mv.setViewName("/common/top");
		try {
//			params.put("loginName", CasPrincipal.getLoginName());
			mv.addObject("customer",ServletUtil.getCustomer(session));
			mv.addObject("cityList",commonService.findTopCityList(params));
		} catch (Exception e) {
			mv.addObject("customer",null);
			log.warn("加载头部用户信息失败:{}",params,e);
		}
		return mv;
	}
	
	@RequestMapping(value={"/top/areaCitys"})
	public ModelAndView getAreaCitys(@RequestParam Map<String,Object> params) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/common/top/areaCitys");
		super.clean(params);
		try {
			mv.addObject("areaCityList",commonService.findAreaCityList(params));
		}catch (Exception e) {
			log.warn("加载头部城市信息失败:{}",params,e);
		}
		return mv;
	}
	public static ModelAndView forwardError(Object msg) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("errorMsg");
		mv.addObject("msg",msg);
		return mv;
	}
	/**
	 * 当前座位已经被选走的错误页
	 * @date 2015年5月26日上午11:04:39
	 */	
	public static ModelAndView forwardErrorPro(String productId) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("errorMsgPro");
		mv.addObject("productId",productId);
		return mv;
	}
	/**
	 * 带返回商品链接的错误页
	 * @date 2015年5月26日上午11:04:39
	 */	
	public static ModelAndView forwardErrorDefaultPro(String productId,Object msg) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("errorMsgDefaultPro");
		mv.addObject("productId",productId);
		mv.addObject("msg",msg);
		return mv;
	}
	@RequestMapping("error")
	public static ModelAndView forwardErrorDefault() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("errorMsg");
		mv.addObject("msg","提交失败！");
		return mv;
	}
	public static ModelAndView forwardErrorDefault(String msg) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("errorMsg");
		mv.addObject("msg",msg);
		return mv;
	}		
	@RequestMapping("errorMsg")
	public static ModelAndView forwardErrorMsg(Integer code) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("errorMsg");
		String msg = "参数异常！";
		if (!StringUtil.isEmpty(code)) {
			if (code == 1) {
				msg = "此订单中包含不可售的票价，不能支付！";
			}else if(code == 2){
				msg = "乐通卡账号或密码错误！";
			}else if(code == 3){
				msg = "提交失败,请仔细核对信息。";
			}
		}
		mv.addObject("msg",msg);
		return mv;
	}	
	
}
